<?php
/**
 * CpController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Admin_IndexController extends Sunnet_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (empty($this->_arrParam['page'])) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (empty($this->_arrParam['line_per_page'])) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
        Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu; 
    }

    public function preDispatch(){
  
        $auth = Zend_Auth::getInstance();
        if ((!$auth->hasIdentity() || !$auth->getIdentity()->admin) && ($this->_arrParam['action'] != 'login')) {
    		$this->_forward('login','index','admin',array('action'	=> 'login'));//,$this->_arrParam);
    	}
        
    }
    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {
         $template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');
//		Load các model và form sử dụng trong action
                
         
    }
    public function loginAction(){
                $template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');
                $option = array('layoutPath'=> $template_path, 'layout'=> 'login');		
		Zend_Layout::startMvc($option);
//		Load các model và form sử dụng trong action
		Zend_Loader::loadClass('Admin_Forms_LoginForm');
		Zend_Loader::loadClass('Admin_Models_LoginModel');		
//		Khởi tạo biến

		$form = new Admin_Forms_LoginForm();
		if ($this->_request->isPost()){
			if ($form->isValid($this->_arrParam)){
    			if (Admin_Models_LoginModel::login($this->_arrParam)){
                                    
                                Zend_Loader::loadClass('Admin_Models_UserAdminModel'); 
                                $auth = Zend_Auth::getInstance();
                                $userinfo_model = new Admin_Models_UserAdminModel();
                                $userinfo = $userinfo_model->loaduserid($auth->getIdentity()->username);
                                $session = new Zend_Session_Namespace();
                               
                                $session->userid = $userinfo[0]['UserID'];
                                $session->userName = $userinfo[0]['username'];
    				$this->view->headMeta()->appendHttpEquiv('Refresh','5;URL=' .$_SERVER['REQUEST_URI']);
    				$this->_redirect($this->view->url());
    			}else{
                                  
    				$this->view->error_msg ='Đăng nhập không thành công';
    			}                        
			}
		}
//		Truyền biến ra view

		$this->view->form = $form;
    }
	public function logoutAction()
	{
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		$curr_url = $this->_request->getParam('curr_url');
                
		if ($curr_url){
    		$this->_redirect($curr_url);
   		}else{
   			$this->_redirect('/admin');
   		}
	}
	
}
